﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 可以对每个文字进行动画的文本控件 
    /// </summary>
    /// <remarks>
    /// （因为TextBlock无法读取到每个文字的位置，故使用自定义控件来实现。****使用TextBlock.ContentStart后会导致TextEffect动画失效的问题暂无法解决。）
    /// </remarks>
    public partial class AnimationTextControl : UserControl
    {
        public AnimationTextControl()
        {
            InitializeComponent();
        }


        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(AnimationTextControl), new PropertyMetadata(null));

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == TextProperty)
            {
                this.basetxt.Inlines.Clear();
                if (!string.IsNullOrEmpty(this.Text))
                {
                    foreach (var item in this.Text)
                    {
                        var textBlock = new TextBlock() { Text = item.ToString() };
                        basetxt.Inlines.Add(textBlock);
                    }
                }
            }
        }

        private void buildAnimation()
        {
            foreach (var item in basetxt.Inlines)
            {
                if (item is InlineUIContainer inui)
                {
                    var word = inui.Child;
                    word.RenderTransform = createTransformGroup();
                }
            }
        }

        private TransformGroup createTransformGroup()
        {
            TransformGroup group = new TransformGroup();
            ScaleTransform scaleTransform = new ScaleTransform(1, 1);
            RotateTransform rotateTransform = new RotateTransform(0);
            TranslateTransform translateTransform = new TranslateTransform(0, 0);
            group.Children.Add(scaleTransform);
            group.Children.Add(rotateTransform);
            group.Children.Add(translateTransform);
            return group;
        }
    }
}
